#region Imports

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using PHSRAG.Insight21.SearchContext;

#endregion Imports

namespace PHSRAG.Insight21.Humans
{
	/// <summary>
	/// The NonStudyStaff object abstracts the notion of people not staffed to a study, but that
	/// have been granted access to study information. It supports loading of summary information 
	/// for NonStaffPerson objects
	/// </summary>
	public class NonStudyStaff : BO.BusinessObject
	{
		#region Constants

		private const string NonStudyStaffKey = "NonStudyStaffKey_0B2B28E6-770B-4dc5-BFE1-C854A430E9DF";
		private const string NonStudyStaffTableName = "nonStudyStaffTableName";

		#endregion

        #region Public Properties

        /// <summary>
		/// Returns the number of people with access to the study
		/// </summary>
		public int Count
		{
			get 
			{
				return (DataContainer.Tables.Count > 0) ? 
					  DataContainer.Tables[NonStudyStaffTableName].Rows.Count : 0;
			}
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Initializes the base class with the specified cache policy.
		/// </summary>
		/// <param name="cachePolicy"></param>
		public NonStudyStaff(ICachePolicy cachePolicy) : base(cachePolicy)
		{
		}
		#endregion

		#region BusinessObject Methods

		/// <summary>
		/// Cache the current instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(NonStudyStaffKey, this);
		}

		/// <summary>
		/// Remove the current instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(NonStudyStaffKey);
		}

		/// <summary>
		/// Load the current instance with summary non-study staff person data for the current protocol
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <param name="args">arguments used to load the instance</param>
		public override void Load(string connectionString, params object[] args)
		{
			if (args.Length < 1)
				throw new Exception("NonStudyStaff load requires a protocolId");

			try 
			{
				DataContainer = DataAccess.GetDataSet(
					connectionString,
					"GetNonStudyStaffWithAccess",
					DataAccess.BuildSqlParameterArray("@protocolId", SqlDbType.Int, args[0],
					"@protocolProcessId", SqlDbType.Int, args[1]));
			}
			catch (Exception ex)
			{
				throw new Exception ("Retrieval of NonStudyStaff failed", ex);
			}

			if (DataContainer.Tables.Count > 0)
				DataContainer.Tables[0].TableName = NonStudyStaffTableName;

		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Returns the set of people not staffed to the study, but that have been granted access
		/// to the study.  Using the dataContainer as the source data, a NonStaffPerson object is 
		/// instantiated per row and added to an ArrayList
		/// </summary>
		/// <returns>Arraylist of StaffPerson objects</returns>
		public ArrayList GetNonStudyStaffList()
		{
			DataTable table = DataContainer.Tables[NonStudyStaffTableName];
			int staffCount = table.Rows.Count;
			ArrayList nonStudyStaff = new ArrayList(staffCount);

			for(int i = 0; i < staffCount; ++i)
				nonStudyStaff.Add(new NonStaffPerson(CachePolicy, table.Rows[i], 0));

			return nonStudyStaff;
		}

		/// <summary>
		/// Returns people that are not staffed to a study and do not have access to the study
		/// </summary>
		/// <param name="connectionString">string containing info to connect to the database</param>
		/// <param name="protocolId">id of the protocol to query</param>
		/// <returns>records of people not staffed to the protocol/study</returns>
		public static DataSet GetNonStudyStaffWithoutAccess(string connectionString, int protocolId, 
			int protocolProcessId, string lastName, string firstName, string username, int orgId)
		{	
			try 
			{
				return DataAccess.GetDataSet(
					connectionString,
					"GetNonStudyStaffWithoutAccess", 
					Utility.DataAccess.BuildSqlParameterArray(
					"@protocolId", SqlDbType.Int, protocolId,
					"@protocolProcessId", SqlDbType.Int, protocolProcessId,
					"@lastName", SqlDbType.VarChar, (lastName == string.Empty) ? (object)DBNull.Value : (object)lastName,
					"@firstName", SqlDbType.VarChar, (firstName == string.Empty) ? (object)DBNull.Value : (object)firstName,
					"@userName", SqlDbType.VarChar, (username == string.Empty) ? (object)DBNull.Value : (object)username,
					"@organizationId", SqlDbType.Int, orgId));
			}
			catch (Exception ex) 
			{
				throw new Exception ("Retrieval of NonStudyStaffWithout Access failed", ex);
			}
		}

		#endregion

	}
}
